package com.liyongzhen.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liyongzhen.pojo.Dept;
import com.liyongzhen.pojo.Result;
import com.liyongzhen.service.DeptService;

import java.util.List;

@Slf4j
//private static Logger log=LoggerFactory.getLogger(DeptController.class);
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

/*查询部门数据*/
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求方式为get
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /*删除功能*/
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门：{}",id);
        //调用service删除
        deptService.delete(id);
        return Result.success();
    }

    /*插入功能*/
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门:{}",dept);
        //调用service方法
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result query(@PathVariable Integer id){
        log.info("查询部门：{}",id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
